package com.ly.heart.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.utils.DateUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.FileEntity;
import com.ly.heart.domain.SurveyReportEntity;
import com.ly.heart.domain.SurveyReportRoofEntity;
import com.ly.heart.domain.dto.SurveyReportDto;
import com.ly.heart.mapper.SurveyReportMapper;
import com.ly.heart.service.IFileInfoService;
import com.ly.heart.service.ISurveyReportRoofService;
import com.ly.heart.service.ISurveyReportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 勘测报告Service业务层处理
 * @author wutao
 * @date 2024-05-22
 */
@Service
public class SurveyReportServiceImpl extends ServiceImpl<SurveyReportMapper, SurveyReportEntity> implements ISurveyReportService {

    @Resource
    private SurveyReportMapper erpSurveyReportInfoMapper;
    @Resource
    private ISurveyReportRoofService iSurveyReportRoofService;
    @Resource
    private IFileInfoService iFileInfoService;

    /**
     * 查询勘测报告
     * @param id 勘测报告ID
     * @return 勘测报告
     */
    @Override
    public SurveyReportDto selectErpSurveyReportInfoById (String id) {
        SurveyReportDto surveyReportDto = new SurveyReportDto ();
        SurveyReportEntity surveyReportEntity = erpSurveyReportInfoMapper.selectErpSurveyReportInfoById ( id );
        //处理所有附件
        List<FileEntity> fileEntities = iFileInfoService.list ( new QueryWrapper<FileEntity> ().eq ( "relation_id", id ) );
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            surveyReportEntity.setFileEntities ( fileEntities );
        }
        surveyReportDto.setSurveyReportEntity ( surveyReportEntity );
        SurveyReportRoofEntity surveyReportRoofEntity = new SurveyReportRoofEntity ();
        surveyReportRoofEntity.setReportId ( id );
        List<SurveyReportRoofEntity> surveyReportRoofEntities = iSurveyReportRoofService.selectErpSurveyReportRoofInfoList ( surveyReportRoofEntity );
        if ( surveyReportRoofEntities != null && surveyReportRoofEntities.size () > 0 ) {
            surveyReportDto.setSurveyReportRoofEntities ( surveyReportRoofEntities );
        }
        return surveyReportDto;
    }

    /**
     * 查询勘测报告列表
     * @param erpSurveyReportInfo 勘测报告
     * @return 勘测报告
     */
    @Override
    public List<SurveyReportEntity> selectErpSurveyReportInfoList (SurveyReportEntity erpSurveyReportInfo) {
        return erpSurveyReportInfoMapper.selectErpSurveyReportInfoList ( erpSurveyReportInfo );
    }

    /**
     * 新增勘测报告
     * @param surveyReportDto 勘测报告
     * @return 结果
     */
    @Override
    public int insertErpSurveyReportInfo (SurveyReportDto surveyReportDto) {
        //1.处理查勘报告基础信息
        SurveyReportEntity surveyReportEntity = surveyReportDto.getSurveyReportEntity ();
        String surveyReportId = SnowflakeIdWorker.getInstance ().nextStringId ();
        surveyReportEntity.setId ( surveyReportId );
        surveyReportEntity.setCreateTime ( DateUtils.getNowDate () );
        //2.处理勘测报告屋顶状况明细
        saveReportRoofEntities ( surveyReportDto.getSurveyReportRoofEntities (), surveyReportId );
        //3.处理附
        updateFile ( surveyReportEntity.getFileEntities (), surveyReportId );
        return erpSurveyReportInfoMapper.insertErpSurveyReportInfo ( surveyReportEntity );
    }

    /**
     * 修改勘测报告
     * @param surveyReportDto 勘测报告
     * @return 结果
     */
    @Override
    public int updateErpSurveyReportInfo (SurveyReportDto surveyReportDto) {
        //1.处理基础信息
        SurveyReportEntity surveyReportEntity = surveyReportDto.getSurveyReportEntity ();
        surveyReportEntity.setUpdateTime ( DateUtils.getNowDate () );
        //2.处理屋顶状况
        List<SurveyReportRoofEntity> surveyReportRoofEntities = surveyReportDto.getSurveyReportRoofEntities ();
        saveReportRoofEntities ( surveyReportRoofEntities, surveyReportEntity.getId () );
        //3.处理附件
        updateFile ( surveyReportEntity.getFileEntities (), surveyReportEntity.getId () );
        return erpSurveyReportInfoMapper.updateErpSurveyReportInfo ( surveyReportEntity );
    }

    /**
     * 批量删除勘测报告
     * @param ids 需要删除的勘测报告ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyReportInfoByIds (String[] ids) {
        return erpSurveyReportInfoMapper.deleteErpSurveyReportInfoByIds ( ids );
    }

    /**
     * 删除勘测报告信息
     * @param id 勘测报告ID
     * @return 结果
     */
    @Override
    public int deleteErpSurveyReportInfoById (String id) {
        return erpSurveyReportInfoMapper.deleteErpSurveyReportInfoById ( id );
    }


    /**
     * 处理勘测报告屋顶状况列表明细
     * @param surveyReportRoofEntities
     * @param surveyReportId
     */
    private void saveReportRoofEntities (List<SurveyReportRoofEntity> surveyReportRoofEntities, String surveyReportId) {
        if ( surveyReportRoofEntities != null && surveyReportRoofEntities.size () > 0 ) {
            //删除屋顶状况
            iSurveyReportRoofService.deleteSurveyReportRoofByReportId ( surveyReportId );
            for ( SurveyReportRoofEntity surveyReportRoofEntity : surveyReportRoofEntities ) {
                surveyReportRoofEntity.setReportId ( surveyReportId );
                int i = iSurveyReportRoofService.insertErpSurveyReportRoofInfo ( surveyReportRoofEntity );
                if ( i == 0 ) {
                    throw new RuntimeException ( "新增勘测屋顶状况失败" );
                }
            }
        }
    }


    /**
     * 修改勘测基础模块的附件，更新relationId为勘测记录Id
     * @param fileEntities
     * @param relationId
     */
    private void updateFile (List<FileEntity> fileEntities, String relationId) {
        if ( fileEntities != null && fileEntities.size () > 0 ) {
            fileEntities.forEach ( fileEntity -> {
                fileEntity.setRelationId ( relationId );
                fileEntity.setChangeTime ( DateUtils.getNowDate () );
            } );
            boolean b = iFileInfoService.updateBatchById ( fileEntities );
            if ( ! b ) {
                throw new RuntimeException ( "修改勘测基础数据附件失败" );
            }
        }
    }

}
